package com.qijian.flowable.controller;

import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.enums.BusinessType;
import com.qijian.flowable.domain.Matrix;
import com.qijian.flowable.domain.query.MatrixQuery;
import com.qijian.flowable.service.IMatrixService;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
/**
 * 流程矩阵Controller
 *
 * @author qijian
 * @date 2022-02-08
 */
@Api(tags = "流程矩阵")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/matrix")
public class MatrixController extends BaseController {

    private final IMatrixService matrixService;

    /**
     * 查询流程矩阵列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:list')")
    @GetMapping("/list")
    public TableDataInfo list(MatrixQuery query, IPage<Matrix> page) {
        return getDataTable(matrixService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出流程矩阵列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:export')")
    @Log(title = "流程矩阵" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(MatrixQuery query, HttpServletResponse response) {
        new ExcelUtil<>(Matrix. class).exportExcel(response,matrixService.list( WhereEntityTool.invoke(query)), "流程矩阵数据");
    }

    /**
     * 获取流程矩阵详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:query')")
    @GetMapping(value = "/{matrixId}")
    public AjaxResult getInfo(@PathVariable("matrixId") Long matrixId) {
        return AjaxResult.success(matrixService.getById(matrixId));
    }

    /**
     * 新增流程矩阵
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:add')")
    @Log(title = "流程矩阵" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Matrix matrix) {
        return toAjax(matrixService.save(matrix));
    }

    /**
     * 修改流程矩阵
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:edit')")
    @Log(title = "流程矩阵" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Matrix matrix) {
        return toAjax(matrixService.updateById(matrix));
    }

    /**
     * 删除流程矩阵
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:remove')")
    @Log(title = "流程矩阵" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{matrixIds}")
    public AjaxResult remove(@PathVariable List<Long> matrixIds) {
        return toAjax(matrixService.removeByIds(matrixIds));
    }

    /**
     * 多人力资源tree
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:list')")
    @GetMapping("/tree")
    public AjaxResult treeListVo() {
        return AjaxResult.success(matrixService.matrixListVo());
    }
}
